/*->c.tekact */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>

#include "h.os"
#include "h.wimp"
#include "h.flex"

#include "h.DrawLevel0"


#include "h.def"
#include "h.wos"
#include "h.main"
#include "h.ram"
#include "h.mym"
#include "h.pr"

#include "h.serial"

#include "h.term"
#include "h.replay"


#include "h.vtdef"
#include "h.vtkey"
#include "h.vtsend"
#include "h.vtwimp"
#include "h.vtscr"
#include "h.vtcon"
#include "h.vtlo"
#include "h.vtcol"
#include "h.vtact"



#include "h.tekint"
#include "h.tek"




/*****************************************************************************/


int tekposn(int byte)
{
 int hibits;
 int lobits;

 hibits=byte & 0x60;
 lobits=byte & 0x1F;

 switch(hibits)
 {
  case 0x40: /* LO X */     /* always sent, last, so finish here */
            if(tekaccseq)
             tekc.y=(tekc.y & 0xFFFFFF83)+(tekaccbits<<2);

            tekc.x=(tekc.x & 0xFFFFFF83)+(lobits<<2);
            tekaccseq=0;
            return(1);

  case 0x20: /* HI Y | HI X */
            if(tekaccseq) 
             tekc.y=(tekc.y & 0xFFFFFF83)+(tekaccbits<<2);

            if(tekaccseq)
             tekc.x=(tekc.x & 0x7F)+(lobits << 7);
            else
             tekc.y=(tekc.y & 0x7F)+(lobits << 7);
            break;

  case 0x60: /* LSB | LO Y */     /* EXTRA */
            if(tekaccseq)
            {
             /* LSB */
             tekc.x=(tekc.x & 0xFFFFFFFC)+(tekaccbits & 0x3);
             tekc.y=(tekc.y & 0xFFFFFFFC)+((tekaccbits & 0xC)>>2);
            }
            tekaccbits=lobits;
            tekaccseq=1;
            break;
 }

 return(0);
}




static void setcharstate(void)
{
 tekstate=TEKCHAR;
}


static void setposnstate(void)
{
 tekstate=TEKPOSN;
}



static void setparamstate(void)
{
 tekstate=TEKPARA;
 tekparam=0;
 tekparams=0;
}



/* return 1 when paramater complete */

int tekgetparam(int byte)
{
 if(byte & 0x40)
 {
  tekparam=tekparam<<6;
  tekparam=tekparam | ((byte & 0x3F)<<4);
  return(0);
 }
 else
 if(byte & 0x20)
 {
  tekparam+=byte & 0xF;
  if(!(byte & 0x10)) tekparam=-tekparam;
  tekparams++;
  return(1);
 }
 return(-1);
}





static void setstrstate(void)
{
 tekstate=TEKSTR;
 tekstrp=0;
 tekstr[0]=0;
 tekstrlen=tekparam;
}



/* return 1 when paramater complete */

int tekgetstr(int byte)
{
 tekstr[tekstrp++]=byte;
 tekstr[tekstrp]=0;
 if(tekstrp==tekstrlen) return(1);
 else
 if((tekstrp+1)==TEKSTRSIZE) return(-1);
 else                        return(0);
}


void teksendint(int i)
{
 int b0;
 int b1;
 int b2;

 b0=b1=b2=0x20;

 b0|=((i>>10) & 0x3F);
 b1|=((i>>4 ) & 0x3F);
 b2|=(i & 0xF);

 if(i>=0) b2|=0x10;

 outbyte(b0);
 outbyte(b1);
 outbyte(b2);
}


/*****************************************************************************/


void tekcls(void)
{
 if(replayfix)
 {
  replayclscode=TEKCLS;
  return;
 }

 tekstart();
 tekclg();
}



/* gin mode */

void tekginmode(void)
{
 tekmode='G';
 tekc.curs=TEKGINCURS;
 if(replayfix) replayclscode=TEKGIN;
}



void tekalpha(int byte)
{
 if(vttekmode && tektextplane) vt220byte(byte);
 else
 switch(byte)
 {
  case     7:
  case 0x107:
             beep();
             break;

  case   127:
             if(tekc.x>=tekdx-1) tekc.x-=tekdx;
             break;

  case     8:
  case 0x108:
             if(tekc.x>=tekdx) tekc.x-=tekdx;
             break;

  case     9:
  case 0x109:
             if(tekc.x<TEKW-2*tekdx) tekc.x+=tekdx;
             break;

  case    10:
             if(tekc.y>=tekdy) tekc.y-=tekdy;
             break;

  case    11:
             case 0x10B:
             if(tekc.y<TEKVISH-tekdy) tekc.y+=tekdy;
             break;

  case    13:
             tekc.x=0;
             break;

     default:
             if(byte>31 && byte<127)
             {
              tekchar(tekc.x,tekc.y,byte);
              tekc.x+=tekdx;
              if(tekc.x>TEKW-tekdx)
              {
               tekc.x=0;
               tekc.y-=tekdy;
               if(tekc.y<0) tekc.y=TEKVISH-tekdy;
              }
             }
             break;
 }


}





void tekvect(int byte)
{
 switch(byte)
 {
   case    13:
              if(!tektextplane)
              {
               tekmode='A';
               teksetlinestyle(0);
              }
              break;

   case  0x1D:
   case 0x11D:
              tekmov.x=-1;
              tekmov.y=-1;
              break;

   default   :
              if(byte>31 && byte<128) 
               if(tekposn(byte))
               {
                if(tekmov.x!=-1)
                {
                 tekmove(tekmov.x,tekmov.y);
                 tekdraw(tekc.x,tekc.y);
                }
                tekmov=tekc;
               }
 }
}





/* point plot mode is the same as marker mode! */

void tekpoi(int byte)
{
 if(byte>31 && byte<128)
   if(tekposn(byte)) tekplot(tekc.x,tekc.y);
}





void tekipoi(int byte)
{
 switch(byte)
 {
  case  32:
           tekipen=0;
           return;

  case 'P':
           tekipen=1;
           return;

  case 'A':
           tekc.x+=4;
           break;

  case 'E':
           tekc.x+=4;
           tekc.y+=4;
           break;

  case 'D':
           tekc.y+=4;
           break;

  case 'F':
           tekc.x-=4;
           tekc.y+=4;
           break;

  case 'B':
           tekc.x-=4;
           break;

  case 'J':
           tekc.x-=4;
           tekc.y-=4;
           break;

  case 'H':
           tekc.y-=4;
           break;

  case 'I':
           tekc.x+=4;
           tekc.y-=4;
           break;

   default:
           tekmode='A';
           tekt(byte);
           return;
 }
 if(tekipen) tekplot(tekc.x,tekc.y);
}




void tekbypass(int byte)
{
 if(byte==tekbypasschar) tekbypassmode=0;
}


/* set bypass mode, do this if bypasschar not NUL */

void teksetbypassmode(void)
{
 if(tekbypasschar!=NUL) tekbypassmode=1;
}



void echoplex(int byte)
{
 if(echostring[echopoi]!=byte)
 {
  tekmode='A';
  tekt(byte);
 }
 echopoi++;
 if(echopoi==strlen(echostring)) tekmode='A';
}



void tekrep2(int byte)
{
 int i;
 echostring[0]=byte;
 echostring[2]=((tekc.x >>2) & 0x1F)+0x20;
 echostring[1]=((tekc.x >>7) & 0x1F) +0x20;
 echostring[4]=((tekc.y >>2) & 0x1F)+0x20;
 echostring[3]=((tekc.y >>7) & 0x1F) +0x20;
 echostring[5]=13;
 if(!localmode) for(i=0;i<6;i++) outbyte(echostring[i]);
 echopoi=0;
}



void tekrep(void)
{
 if(tekmode=='A') tekrep2(0x24);
 else             tekrep2(0x20);
}


/*****************************************************************************/


/* set error threshold */
/* ESC KT error threshold (integer, 0), in range 0-4 */


static void tekerrth(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/* saves non volatile params that have been altered */
/* ESC KU */

static void teksavenvparams(void)
{
 tekstate=TEKC1;
}






/* set scaling mode */
/* ESC ZS */

static void tekscalemode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}







/* enable text plane */
/* ESC KA enable-mode (integer 1) */
/* 1==enabled 0==not */

static void tekenabletextplane(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               tekdoenabletextplane(tekparam);
               break;
 }
}





/* set tab stops */
/* ESC KB tab-posns (integer array) */

static void teksettabs(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) /* first array param no of params */
               {
                paramlt=tekparam;
               }
               else
               if(tekparams>1)  /* sort out params */
               {
                if(tekparam>=0 && tekparam<TTYCOLS)
                {
                 tabs[tekparam]=1;
                 vtrefreshtabs();
                }
               }
               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}



/* set text plane size                     */
/* ESC LL number of lines (integer, error) */

static void tektextplanesize(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/* set dialogue area write mode              */
/* ESC LM mode (integer, 0)                  */
/* 1 means SPC and _ overstrike, not replace */

static void tekdialogwmode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* set text plane colour */
/* ESC LI character index (integer, 0) background index (integer, 0) */
/*                  dialog background index (integer, 0)             */

static void tektextplanecolour(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}





/* vary text plane visibility */
/* ESC LV mode (integer, 1)   */
/* 1==visible 0==not          */

static void tektextplanevisi(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/* vary graphics plane visibility */
/* ESC ZG */

static void tekgraphplanevisi(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/* clears the text plane */
/* ESC LZ */

static void tekclrtext(void)
{
 if(tektextplane) clearscrhome(VTFXNONE);
 tekstate=TEKC1;
}







/* set gin cursor colour */
/* ESC TC hue, lightness, saturation, 3 x integer, 0 */

static void teksetgincolour(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}





/* text cursor colour index */
/* ESC TD first colour (integer, 0) second colour (integer, 0) */

static void tektextcursorcol(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==2) tekstate=TEKC1;
               break;
 }
}






/* Direct draw */
/* ESC LF position (posn 0,0) */

static void tekcodraw(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               tekmov=tekc;
               setposnstate();
               break;

  case TEKPOSN:
               tekstate=TEKC1;
               tekmove(tekmov.x,tekmov.y);
               tekdraw(tekc.x,tekc.y);
               break;
 }
}







/* Direct move */
/* ESC LG position (posn 0,0) */

static void tekcomove(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setposnstate();
               break;

  case TEKPOSN:
               tekstate=TEKC1;
               tekmov=tekc;
               break;
 }
}







/* set line/marker colour    */
/* ESC ML index (integer, 0) */

static void tekcocolour(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               teksetindex(&teklineindex,tekparam);
               break;
 }
}





/* set line style                 */
/* ESC MV line-style (integer, 0) */
/* in range 0 to 7                */

static void tekcoline(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               teksetlinestyle(tekparam);
               break;
 }
}







/* Direct plot of marker */
/* ESC LH position (posn, 0,0) */

void tekcomarker(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setposnstate();
               break;

  case TEKPOSN:
               tekstate=TEKC1;
               tekplot(tekc.x,tekc.y);
               break;
 }
}





/* set marker type */
/* ESC MM marker-number (integer, 0) */
/* in range 0-10 */

static void tekmarkertype(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               tekmarker=tekparam;
               if(tekmarker<0)  tekmarker=0;
               if(tekmarker>10) tekmarker=10;
               break;
 }
}




/* start drawing panel                                            */
/* ESC LP <coords> <draw boundary (int)>                          */
/* default 0,0 and 0                                              */
/* draw boundary, 1 show boundary, 0 fill covers boundary         */
/* boundary specified by draw and moves, or marker or vector mode */

static void tekpanelstart(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               tekpanel=1;
               tekpanelx=tekpanely=0;
               tekpanelboundary=0;
               setposnstate();
               tekstartpanel();
               break;

  case TEKPOSN:
               setparamstate();
               tekpanelx=tekc.x;
               tekpanely=tekc.y;
               break;

  case TEKPARA:
               tekpanelboundary=tekparam;
               tekstate=TEKC1;
               break;
 }
}







/* set dialog buffer size         */
/* ESC LB size (intger, error)    */

static void teksetdbuffsize(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}






/* end panel                                      */
/* ESC LE panel boundary closed, panel filled     */
/* graphics posn is set to the panels first point */

static void tekpanelend(void)
{
 if(tekpanel)
 {
  tekmove(tekc.x,tekc.y);
  tekdraw(tekc.x=tekpanelx,tekc.y=tekpanely);
  tekendpanel();
 }
 tekpanel=0;
 tekstate=TEKC1;
}





/* fill area */
/* ESC ZF */

void tekfillarea(void)
{

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* set fill colour */
/* ESC MP fill pattern number (integer, 0)      */
/* -15 to 0 for solid colours, +ve for patterns */

static void teksetfillcolour(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               if(tekparam<0) tekparam=-tekparam;
               if(tekparam>0) tekparam&=0xF;
               teksetindex(&tekfillindex,tekparam);
               break;
 }
}





/* ?????? */
/* ESC MQ integer - unknown  */

static void tekxxxx(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* write a string of graph text       */
/* ESC LT text (string, empty string) */

static void tekwritetext(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               setstrstate();
               break;

  case  TEKSTR:
               tekstring(tekc.x,tekc.y,tekstr);
               tekstate=TEKC1;
               break;
 }
}





/* set text index */
/* ESC MT index (integer, 0) */
/* sets the colour index for graph text and alpha text in graph plane */

static void teksetgraphcolour(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               teksetindex(&tekfontindex,tekparam);
               break;
 }
}





/* sets size for graph text, but does not effect alpha text       */
/* ESC MC unused (integer), height (integer, 61), unused(integer) */

static void teksetgraphsize(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==2) tekfontsize=tekparam;
               else
               if(tekparams==3) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}





/* sets mode for writing text in alpha area */
/* ESC MG mode (integer, 0)  */
/* 1==overstrike, 0==replace */

static void teksetgraphicstextmode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}



/* sets slant for graph text */
/* ESC MA integer */

static void teksetgraphslant(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* sets rotation for graph text */
/* ESC MR mantissa (integer, 0) power of two (integer, 0) */
/* angle, usually 0,90,180,270 */
/* power of two, usually zero  */


static void teksetgraphrot(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==2) tekstate=TEKC1;
               break;
 }
}




/* specifies the direction to move in after writing each graph char */
/* ESC MN direction (integer, 0)                    */
/* 0 == path at 0 degrees to roatation angle        */
/* 1 == path at +180 degrees to roatation angle     */
/* 2 == path at +90 degrees to roatation angle      */
/* 3 == path at -90 degrees to roatation angle      */
/* this means text can go R to L +180 or down or up */

static void teksetgraphpath(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}


/* draw circle */
/* ESC ZC radius (integer) */

static void tekcocircle(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekcircle(tekc.x,tekc.y,tekparam);
               tekstate=TEKC1;
               break;
 }
}




/* redefine colour map */
/* ESC TG surface-number (integer, must be 1) mixture (integer array) */

/* param 0 index */
/* then RGB, CMY, HLS */


static void tekredefinecolmap(void)
{
 static int paramlt;
 static int index[4];
 
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               paramlt=3;        /* need to skip cuts */
               break;

  case TEKPARA:
               if(tekparams==2) paramlt=tekparam+2;
               else
               if(tekparams>2)
               {
                index[(tekparams-3) & 0x3]=tekparam;
                if(((tekparams-3) & 0x3)==3)
                                      tekdefineindex(index,tekdefcolourmode);
               
               }
               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}




/* Set dialog area colour map */
/* ESC TF integer array       */

static void tekdialogcolmap(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) paramlt=tekparam;
               else
               {




               }
               if(tekparams>=paramlt) tekstate=TEKC1;
               break;
 }
}








/* sets colour mode */
/* ESC TM mode, integer, intger */
/* 1==RGB 2==CMY 3==HLS */

static void tekcolourmode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1)
               {
                if(tekparam<1) tekparam=1;
                if(tekparam>3) tekparam=3;
                tekdefcolourmode=tekparam;
               }
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}



/* ESC ZR, Microcolour */
/* default colour map  */

static void tekdefcolourmap(void)
{
 tekstate=TEKC1;
 if(!teklockcols) tekdefcolours();
}




/* set background colour */
/* ESC RA surface (integer, 1 or -1) index (integer, error) unused (integer) */

static void tekbakcol(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==2) teksetindex(&tekbackindex,tekparam);
               if(tekparams==3) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}





/* set pixel beam posn */
/* ESC RH posn */

static void teksetpixelbeamposn(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setposnstate();
               break;

  case TEKPOSN:
               tekstate=TEKC1;
               break;
 }
}





/* set pixel view port */
/* ESC RS posn posn    */

static void teksetpixelviewport(void)
{
 static int posns;

 switch(tekstate)
 {
  case   TEKC2:
               setposnstate();
               posns=0;
               break;

  case TEKPOSN:
               if(++posns==2) tekstate=TEKC1;
               break;
 }
}







/* Begin pixel operations */
/* three integer params   */
/* ESC RU */

static void tekbeginpixop(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}




/* set window, defines window on terminal space */
/* ESC RW posn posn */

static void teksetwindow(void)
{
 static int posns;

 switch(tekstate)
 {
  case   TEKC2:
               setposnstate();
               posns=0;
               break;

  case TEKPOSN:
               if(++posns==2) tekstate=TEKC1;
               break;
 }
}




/* pixel copy */
/* ESC RX integer, posn, posn, posn */

static void tekpixcopy(void)
{
 static int posns;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               posns=0;
               setposnstate();
               break;

  case TEKPOSN:
               if(++posns==3) tekstate=TEKC1;
               break;
 }
}






/* raster write */
/* ESC RP number of pixels (integer, error) string */


static void tekrasterw(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               setstrstate();
               break;

  case  TEKSTR:
               tekstate=TEKC1;
               break;

 }
}









/* run length write */
/* ESC RL run code array (empty) */

static void tekrlwrite(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) paramlt=tekparam;
               if(tekparams>=paramlt) tekstate=TEKC1;
               break;
 }
}








/* pixel rectangle fill */
/* ESC RR posn (position, 0) posn (position, 0) index (integer 0) */


static void tekpixrect(void)
{
 static int posns;

 switch(tekstate)
 {
  case   TEKC2:
               setposnstate();
               posns=0;
               break;

  case TEKPOSN:
               if(++posns==2) setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}









/* copy input to printer until eof string or cancel key        */
/* ESC JC source(string),separator(string),destination(string) */
/*        HO:            to                 HC:                */


static void tekhardcopy(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}








/* go into alpha mode, cancel hard copy */
/* unlock keyboard... */
/* ESC KC */

static void tekcancel(void)
{
 tekstate=TEKC1;
 tekmode='A';
 keylockmode=0;
}




/* reset terminal */
/* ESC KV */

static void tekreset(void)
{
 tekstate=TEKC1;
 tekdoreset();
}




/* do a dump, para is type 0,1=-ve, 2=+ve 3-ve text plane only */
/* ESC KH hardcopy-code (integer, 0) */

static void tekcodump(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               tekdump(tekparam<2);
               break;
 }
}




/* hard copy attributes 0=mono 1,2=colour */
/* ESC QD copier type (integer, 0) */

static void tekcopyattributes(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}






/* hard copy dialog attributes */
/* ESC QL three x (integer, 0) */

static void tekdialogcopyattributes(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}






/* set copy size */
/* ESC QA size (integer, 0) */

static void teksetcopysize(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}






/* size of gin cursor 0=small 1=full screen */
/* ESC ZI */

void tekginsize(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/* Specifies if a CR implies a LF       */
/* ESC KR crlf-mode(integer), default 0 */
/* 1==CR implies LF, LF is not sent     */


void tekcrlf(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* sets snoopy mode */
/* ESC KS snoopy-mode (integer, 0) */

static void teksetsnoopy(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}






/* Specifies if a LF implies a CR       */
/* ESC KR lfcr-mode(integer), default 0 */
/* 1==LF implies CR, CR is not sent     */


void teklfcr(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}









/* define macro */
/* ESC KD macro number (integer, 0), contents (integer array, empty) */

static void tekdefinemacro(void)
{
 static int paramlt;


/* dprintf(0,"tekstate=%d paramlt=%d tekparams=%d tekparam=%d           ",tekstate,paramlt,tekparams,tekparam); */


 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               paramlt=3;        /* need to skip cuts */
               break;

  case TEKPARA:
               if(tekparams==2) paramlt=tekparam+2;
               else
               if(tekparams>2)
               {



               }

               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }

/* dprintf(1,"tekstate=%d paramlt=%d tekparams=%d tekparam=%d           ",tekstate,paramlt,tekparams,tekparam); */

}






/* set echo mode                 */
/* ESC KE echo-mode (integer, 1) */
/* 1==yes local echo             */

static void teksetecho(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               echomode=(tekparam==1);
               tekstate=TEKC1;
               break;
 }
}









/* set ignore delete mode */
/* ESC KI mode (integer, 1) */


static void tekigndel(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               tekignoredels=1;
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               tekignoredels=tekparam;
               break;
 }
}








/* define non-volatile macro */
/* ESC KO macro number (integer, 0), contents (integer array, empty) */


static void tekdefinenvmacro(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               paramlt=3;        /* need to skip cuts */
               break;

  case TEKPARA:
               if(tekparams==2) paramlt=tekparam+2;
               else
               if(tekparams>2)
               {



               }

               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}






/* report errors */
/* ESC KQ */

static void tekreporterrors(void)
{
 tekstate=TEKC1;
}






/* enable key expansion */
/* ESC KW macro number (integer, 1) 1==enabled 0==not */

static void tekenablemacros(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               tekmacrosenabled=tekparam;
               break;
 }
}








/* expand macro */
/* ESC KX macro number (integer, 0) */

static void tekexpandmacro(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* set key execute character         */
/* ESC KY key-exec-char (integer, 0) */

static void teksetkeyexecchar(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}



/* set edit chars      */
/* ESC KZ  3 x integer */

static void tekseteditchars(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==3) tekstate=TEKC1;
               break;
 }
}



/* params are first step for normal keys, second for shift */
/* ESC IJ normal (integer, 1) shifted (integer, 1)         */
/* in range 1-10, 1==slow 10==fast                         */

static void tekginstep(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==2)
               {
                tekstate=TEKC1;
                tekgsteps=tekparam;
               }
               else
               {
                tekgstepn=tekparam;
                tekparam=0;
               }
               break;
 }
}





/* set posn that gin cursor will appear at */
/* ESC SX segment number (intger, 0)  posn (posn, 0) */

static void tekginposn(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               setposnstate();
               break;

  case TEKPOSN:
               tekstate=TEKC1;
               tekgin=tekc;
               break;
 }
}







/* set characters used to terminate bypass mode */
/* ESC NU bypass-cancel-character (integer, 0) */
/* factory LF */

static void teksetbypass(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekbypasschar=tekparam;
               tekstate=TEKC1;
               break;
 }
}





/* sets tek mode 0=TEK 1,2=ANSI 3=VT52 */
/* ESC % ! syntax (integer, 0) */


static void tekcomode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               if(tekparam)
               {
                vtquittekmode();            /* what if in GIN */
                ansimode=tekparam!=3;


               }
               break;
 }
}






/* send settings report that contains the syntax mode status to the host */
/* ESC # ! 0 */


static void tekreportsyntaxmode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setcharstate();
               break;

  case TEKCHAR:
               tekstate=TEKC1;
               break;
 }
}




/* report terminal settings */
/* ESC I Q char char */

static void tekreportsettings(void)
{
 static int nc;
 static int first;

 switch(tekstate)
 {
  case   TEKC2:
               setcharstate();
               nc=0;
               break;

  case TEKCHAR:
               if(nc==0) first=tekbyte;
               else
               if(nc==1)
               {
                if(first=='?' && tekbyte=='T')
                {
                 outbyte('?');
                 outbyte('T');
                 teksendint(tekversion);
                 outbyte(tekfirsteom);
                }
               }

               nc++;
               if(nc==2) tekstate=TEKC1;
               break;
 }
}









/* enter prompt mode               */
/* ESC NM prompt mode (integer, 0) */
/* 1==enter 0==exit                */


static void tekprompt(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }

}




/* specifies tx parity                         */
/* ESC NP parity (integer, 0)                  */
/* 0==none 1==odd 2==even 3==set 4==8 bit mode */


static void teksetparity(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}



/* sets size of inout buffer */
/* ESC NQ size (integer, 0)  */

static void teksetqueue(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}







/* set baud rate */
/* ESC NR txrate (integer, error) rxrate (integer, error) */ 
/* defaults 2400/2400 */

static void tekbaud(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==2) tekstate=TEKC1;
               break;
 }
}









/* specifies the terminals eof string        */
/* ESC NE integer array, max length 2 chars */

static void tekseteofstring(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) /* first array param no of params */
               {
                paramlt=tekparam;
                tekeofstringlen=0;
               }
               else
               if(tekparams>1)  /* sort out params */
               {
                if(tekeofstringlen<TEKSTRLEN1)
                {
                 tekeofstring[tekeofstringlen++]=tekparam;
                }
               }
               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}







/* set flagging mode, flow control... */
/* ESC NF mode (integer, 0) */
/* 0==none 1==Xon receiving 2==Xon transmit 3==both 4==DTR/CTS */


static void teksetflagmode(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/* specifies the terminals prompt string     */
/* ESC NS integer array, max length 10 chars */

static void teksetpromptstring(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) /* first array param no of params */
               {
                paramlt=tekparam;
                tekpromptstringlen=0;
               }
               else
               if(tekparams>1)  /* sort out params */
               {
                if(tekpromptstringlen<TEKSTRLEN1)
                {
                 tekpromptstring[tekpromptstringlen++]=tekparam;
                }
               }
               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}





/* specifies the terminals eol string        */
/* ESC NT integer array, max length 2 chars */

static void tekseteolstring(void)
{
 static int paramlt;

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) /* first array param no of params */
               {
                paramlt=tekparam;
                tekeolstringlen=0;
               }
               else
               if(tekparams>1)  /* sort out params */
               {
                if(tekeolstringlen<TEKSTRLEN1)
                {
                 tekeolstring[tekeolstringlen++]=tekparam;
                }
               }
               if(tekparams>=paramlt) tekstate=TEKC1;
               tekparam=0;
               break;
 }
}






/* sets number of stop bits */
/* ESC NB number of bits (integer, 1) */
/* 1 or 2 */

static void teksetstopbits(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}




/* specifies the terminals 1st and 2nd. eom chars  */
/* ESC NC first (integer, 0) second (integer, 0)   */

static void tekseteomchars(void)
{

 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               if(tekparams==1) 
               {
                tekfirsteom=tekparam;
                tekparam=0;
               }
               else
               if(tekparams>1)
               {
                teksecondeom=tekparam;
                tekstate=TEKC1;
               }
               break;
 }
}




/* set delay after sending one line, before sending the next */
/* ESC ND delay (integer, 0) */


static void teksettxdelay(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}



/* sets duration in millisecs of break */
/* ESC NK break-time (integer, 0) */

static void teksetbreaktime(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}



/* sets max tx baud rate        */
/* ESC NL rate (integer, error) */

static void teksettxratelimit(void)
{
 switch(tekstate)
 {
  case   TEKC2:
               setparamstate();
               break;

  case TEKPARA:
               tekstate=TEKC1;
               break;
 }
}





/****************************************************************************/




void tekcommander(void)
{
 switch(tekfirst)
 {
  case '#':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case '!':
                     tekreportsyntaxmode();
                     break;
   
             default:
                     tekstate=TEKC1;
                     break;
            }
            break;





  case '%':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case '!':
                     tekcomode();
                     break;
   
             default:
                     tekstate=TEKC1;
                     break;
            }
            break;



  case 'I':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case 'J':
                     tekginstep();
                     break;

            case 'Q':
                     tekreportsettings();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
            }
            break;



  case 'J':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case 'C':
                     tekhardcopy();
                     break;
   
             default:
                     tekstate=TEKC1;
                     break;
           }
           break;





  case 'K':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {

            case 'A':
                     tekenabletextplane();
                     break;

            case 'B':
                     teksettabs();
                     break;

            case 'C':
                     tekcancel();
                     break;

            case 'D':
                     tekdefinemacro();
                     break;

            case 'E':
                     teksetecho();
                     break;

            case 'F':
                     teklfcr();
                     break;

            case 'H':
                     tekcodump();
                     break;

            case 'I':
                     tekigndel();
                     break;

            case 'O':
                     tekdefinenvmacro();
                     break;

            case 'Q':
                     tekreporterrors();
                     break;

            case 'R':
                     tekcrlf();
                     break;

            case 'S':
                     teksetsnoopy();
                     break;

            case 'T':
                     tekerrth();
                     break;

            case 'U':
                     teksavenvparams();
                     break;

            case 'V':
                     tekreset();
                     break;

            case 'W':
                     tekenablemacros();
                     break;


            case 'X':
                     tekexpandmacro();
                     break;


            case 'Y':
                     teksetkeyexecchar();
                     break;

            case 'Z':
                     tekseteditchars();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
            }
            break;







  case 'L':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {

            case 'B':
                     teksetdbuffsize();
                     break;

            case 'E':
                     tekpanelend();
                     break;

            case 'G':
                     if(tekpanel) tekcodraw();
                     else         tekcomove();
                     break;

            case 'F':
                     tekcodraw();
                     break;

            case 'H':
                     tekcomarker();
                     break;

            case 'I':
                     tektextplanecolour();
                     break;

            case 'L':
                     tektextplanesize();
                     break;

            case 'M':
                     tekdialogwmode();
                     break;

            case 'P':
                     tekpanelstart();
                     break;

            case 'T':
                     tekwritetext();
                     break;

            case 'V':
                     tektextplanevisi();
                     break;

            case 'Z':
                     tekclrtext();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
           }
           break;







  case 'M':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {

            case 'A':
                     teksetgraphslant();
                     break;

            case 'C':
                     teksetgraphsize();
                     break;

            case 'G':
                     teksetgraphicstextmode();
                     break;

            case 'L':
                     tekcocolour();
                     break;

            case 'M':
                     tekmarkertype();
                     break;

            case 'N':
                     teksetgraphpath();
                     break;

            case 'P':
                     teksetfillcolour();
                     break;

            case 'Q':
                     tekxxxx();
                     break;

            case 'R':
                     teksetgraphrot();
                     break;

            case 'T':
                     teksetgraphcolour();
                     break;

            case 'V':
                     tekcoline();
                     break;


             default:
                     tekstate=TEKC1;
                     break;
            }
            break;



  case 'N':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {

            case 'B':
                     teksetstopbits();
                     break;

            case 'C':
                     tekseteomchars();
                     break;

            case 'D':
                     teksettxdelay();
                     break;

            case 'E':
                     tekseteofstring();
                     break;

            case 'F':
                     teksetflagmode();
                     break;

            case 'K':
                     teksetbreaktime();
                     break;

            case 'L':
                     teksettxratelimit();
                     break;

            case 'M':
                     tekprompt();
                     break;

            case 'P':
                     teksetparity();
                     break;

            case 'Q':
                     teksetqueue();
                     break;

            case 'R':
                     tekbaud();
                     break;

            case 'S':
                     teksetpromptstring();
                     break;

            case 'T':
                     tekseteolstring();
                     break;
   
            case 'U':
                     teksetbypass();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
            }
            break;






  case 'Q':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {

            case 'A':
                     teksetcopysize();
                     break;

            case 'D':
                     tekcopyattributes();
                     break;

            case 'L':
                     tekdialogcopyattributes();
                     break;
   
             default:
                     tekstate=TEKC1;
                     break;
            }
            break;





  case 'R':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case 'A':
                     tekbakcol();
                     break;

            case 'H':
                     teksetpixelbeamposn();
                     break;

            case 'L':
                     tekrlwrite();
                     break;

            case 'P':
                     tekrasterw();
                     break;

            case 'R':
                     tekpixrect();
                     break;

            case 'S':
                     teksetpixelviewport();
                     break;

            case 'U':
                     tekbeginpixop();
                     break;

            case 'W':
                     teksetwindow();
                     break;

            case 'X':
                     tekpixcopy();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
           }
           break;








  case 'S':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case 'X':
                     tekginposn();
                     break;
   
             default:
                     tekstate=TEKC1;
                     break;
            }
            break;








  case 'T':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {

            case 'C':
                     teksetgincolour();
                     break;

            case 'D':
                     tektextcursorcol();
                     break;

            case 'F':
                     tekdialogcolmap();
                     break;

            case 'G':
                     tekredefinecolmap();
                     break;

            case 'M':
                     tekcolourmode();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
            }
            break;












  case 'Z':
           if(tekstate==TEKC1)
           {
            tekstate=TEKC2;
           }
           else
           switch(teksecond)
           {
            case 'C':
                     tekcocircle();
                     break;

            case 'F':
                     tekfillarea();
                     break;

            case 'G':
                     tekgraphplanevisi();
                     break;

            case 'I':
                     tekginsize();
                     break;

            case 'R':
                     tekdefcolourmap();
                     break;

            case 'S':
                     tekscalemode();
                     break;

             default:
                     tekstate=TEKC1;
                     break;
            }
            break;




   default:
           tekstate=TEKC1;
           break;
 }
}



void tekcommand(int byte)
{
/* dprintf(0,"tekcommand"); */

 switch(tekstate)
 {     
  case   TEKC1:
               tekfirst=byte-0x100;
               tekcommander();
               break;

  case   TEKC2:
               teksecond=byte;
               tekcommander();
               break;

  case TEKPOSN:  /* accumulate posn */
               if(tekposn(byte)) tekcommander();
               break;

  case TEKPARA: /* accumulate parameter */
               if(tekgetparam(byte)) tekcommander();
               break;

  case  TEKSTR:
               if(tekgetstr(byte)) tekcommander();
               break;

  case TEKCHAR:
               tekbyte=byte;
               tekcommander();
               break;

 }
}



int tekmaskbyte(int byte)
{
 return(byte & 0x7F);
}


void tekt(int byte)
{ 
 byte=byte & 0x7F;

/*
 dprintf(1,"tekstate=%d tekmode=%c tekbypass=%d",
                                   tekstate,tekmode,tekbypassmode);

 dprintf(2,"tekfirst=%c teksecond=%c",tekfirst,teksecond);

 */


 if(tekstate)
 {
  tekcommand(byte);
  return;
 }
 else
 if(tekmode=='E')
 {
  echoplex(byte);
  return;
 }
 else
 if(tekbypassmode)
 {
  tekbypass(byte);
  return;
 }

 if(byte==ESC)
 {
  tekesc=0x100;
  return;
 }
 else
 {
  byte+=tekesc;
  tekesc=0;
 }

/* dprintf(0,"MAIN SWITCH"); */

 switch(byte)
 {
  case  ESX(ETB):
                 tekdump(0);
                 break;

  case  ESX(SUB):
                 tekginmode();
                 break;

  case  ESX(CAN):
                 teksetbypassmode();
                 break;

  case        FS:
                 tekmode='P';
                 break;

  case        GS:
  case   ESX(GS):
                 tekmode='V';
                 tekmov.x=-1;
                 tekmov.y=-1;
                 break;
          
  case        RS:
  case   ESX(RS):
                 tekmode='I';
                 tekipen=1;
                 break;

  case        US:
                 tekmode='A';
                 break;

  case        FF:
  case   ESX(FF):
                 tekcls();
                 break;

  case   ESX(SI):
  case   ESX(SO):
                 vtgl=ESX(SI)-byte;
                 break;

  case  ESX(ENQ):
                 tekrep();
                 break;




  case  ESX('8'):
  case  ESX('9'):
  case  ESX(':'):
  case  ESX(';'):
                 teksetfontsize(byte-ESX('8'));
                 break;

  case  ESX('`'):      /* ' abcd seq */
  case  ESX('a'):
  case  ESX('b'):
  case  ESX('c'):
  case  ESX('d'):
  case  ESX('e'):
  case  ESX('f'):
  case  ESX('g'):
                 teksetlinestyle(byte-ESX('`'));
                 break;

  case  ESX('h'):      /* ' abcd seq */
  case  ESX('i'):
  case  ESX('j'):
  case  ESX('k'):
  case  ESX('l'):
  case  ESX('m'):
  case  ESX('n'):
  case  ESX('o'):
                 teksetlinestyle(byte-ESX('h'));
                 break;

  case  ESX('p'):      /* pqrstu seq */
  case  ESX('q'):
  case  ESX('r'):
  case  ESX('s'):
  case  ESX('t'):
  case  ESX('u'):
                 teksetlinestyle(byte-ESX('p')+8);
                 break;


  case       CAN:     /* return to VT */
                 if(tektextplane)
                 {
                  if(vttekmode) vtquittekmode();
                  vtsetfocusfront();
                 }
                 break;

         default:
                 if(byte== ESX('Z') && tek4010)
                                       vtconvertstringline("TEK|[/Z");
                 else
                 if(byte>ESX(SPC))     tekcommand(byte);
                 else  
                 {/* dprintf(0,"default switch"); */
                  switch(tekmode)
                  {
                   case 'P':
                            tekpoi(byte);
                            break;

                   case 'V':
                            tekvect(byte);
                            break;

                   case 'I':
                            tekipoi(byte);
                            break;

                   case 'A':
                            tekalpha(byte);
                            break;
                  }
                 }
 }
}





void tekkey(int * key)
{
 tekcstr temp;
 int stepx;
 int stepy;
 int byte=*key;

 stepx=stepy=0;

 if(tekmode!='G') vtkey(key);
 else 
 {
  switch(byte)
  {
   case 0x18C:
              stepx=-tekgstepn;
              break;

   case 0x18D:
              stepx=tekgstepn;
              break;

   case 0x18E:
              stepy=-tekgstepn;
              break;

   case 0x18F:
              stepy=tekgstepn;
              break;

   case 0x19C:
              stepx=-tekgsteps;
              break;

   case 0x19D:
              stepx=tekgsteps;
              break;

   case 0x19E:
              stepy=-tekgsteps;
              break;

   case 0x19F:
              stepy=tekgsteps;
              break;

      default:
              if(byte<0x7F && byte>=0x20)
              {
               tekc.curs=TEKNOCURS; 
               tekrep2(byte);
               tekmode='E';
               *key=-1;
              }
              return;
  }

  stepx+=tekc.x;
  stepy+=tekc.y;

  if(stepx<0) stepx=0;
  if(stepx>TEKW) stepx=TEKW;
  if(stepy<0) stepy=0;
  if(stepy>TEKVISH) stepy=TEKVISH;

  temp.x=stepx;
  temp.y=stepy;
  temp.curs=TEKGINCURS;

  tekupdatecursor(&tekc,&temp);
  tekc=temp;

  teksaveold();
  *key=-1;
 }
}


